require(["common", "coAjax", "queryString", "href", "hash_routie", "Cookies"], function(jSouper, coAjax, QueryString, href, hash_routie, Cookies) {


	/*
	 * 请求购物车数据
	 */
	function _get_cart_success(result) {
		var cart_detail = result.result;

		App.set("user.cart_detail", cart_detail);
		App.set("$Cache.cart.total_price", Model.Observer(function() {
			var cart_detail = App.get("user.cart_detail");
			var total_price = 0;
			cart_detail.forEach(function(cart) {
				cart.goods_info.forEach(function(goods_info) {
					total_price += goods_info.goods.price * goods_info.num;
				});
			});
			return total_price;
		}));

		var cart_id_list = jSouper.map(cart_detail, function(cart) {
			return cart._id;
		});
		if (cart_id_list.length) {
			//计算订单价格
			coAjax.post(appConfig.user.cart_get_cash, {
				//user_address_id
				cart_id_list: cart_id_list,
				recommender_id: recommender_id,
				guide_id: guide_id
			}, function(result) {
				var moni_ach = result.result;
				App.set("$Cache.moni_ach", moni_ach);
			}, function(errorCode, xhr, errorMsg) {
				alert("error", errorMsg);
			});
		}
	};

	function _get_cart_error(errorCode, xhr, errorMsg) {
		console.log("%c" + errorCode + ": " + errorMsg, "color:orange;font-size:14px; text-shadow: 0 1px 0 #ccc;");
		alert("error", errorMsg);
	};


	var queryString = new QueryString();
	var goods_id = queryString.get("goods_id");
	var recommender_id = queryString.get("rcid") || Cookies.get("recommender_id");
	var guide_id = queryString.get("gi") || Cookies.get("guide_id");

	if (goods_id) {
		coAjax.post(appConfig.user.cart, {
			goods_id: goods_id,
			num: queryString.get("num")
		}, _get_cart_success, _get_cart_error);
	} else {
		coAjax.get(appConfig.user.get_cart_detail_by_bus_id, {
			bus_id: App.get("bus_info.bus_id")
		}, _get_cart_success, _get_cart_error);
	}

	App.set("$Event.cart.cart_to_ach", function(e) {
		var cart_detail = App.get("user.cart_detail");
		var cart_id_list = jSouper.map(cart_detail, function(cart) {
			return cart._id;
		});

		coAjax.post(appConfig.user.cart_make_ach, {
			//user_address_id
			cart_id_list: cart_id_list,
			recommender_id: recommender_id,
			guide_id: guide_id
		}, function(result) {
			console.log(result);
			href.jump("/pay-beta.html#?ach_id=" + result.result._id)
		}, function(errorCode, xhr, errorMsg) {
			alert("error", errorMsg);
		});
	});
	App.set("$Event.cart.remove_goods", function(e, vm) {
		coAjax["delete"](appConfig.user.remove_goods_from_cart, {
			goods_id: vm.get("goods_id"),
			cart_id: vm.get("$Parent.$Parent._id")
		}, _get_cart_success, _get_cart_error)
	});
	App.set("$Event.cart.update_cart_num", function(e, vm) {
		coAjax.put(appConfig.user.update_goods_num_in_cart, {
			goods_id: vm.get("goods_id"),
			cart_id: vm.get("$Parent.$Parent._id"),
			num: vm.get("num")
		}, _get_cart_success, _get_cart_error)
	})
		// App.get("$Event.cart.remove_cart",function (e,vm) {
		// 	coAjax["delete"](appConfig.user.remove_cart,{
		// 		cart_id:vm.get("_id")
		// 	},_get_cart_success,_get_cart_error)
		// });
});